﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CUiMFVariationsOptionsDlg
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;


namespace DnProcessSimulateCommands.ModularFixture;

public class CUiMFVariationsOptionsDlg : TxForm
{
  private const string DEFAULT_OPTION_NAME = "Variation";
  private const int MAX_SIZE = 1000;
  private TextBox editBox;
  private int m_itemSelected;
  private ArrayList m_options;
  private IContainer components;
  private ListBox m_optionsList;
  private Button m_newBtn;
  private Button m_deleteBtn;
  private Button m_ok;
  private Button m_cancel;

  public CUiMFVariationsOptionsDlg(string variationName, ArrayList options)
  {
    this.InitializeComponent();
    ((Control) this).Text = $"{CApMFAdminUtils.m_resourceManager.GetString("EDIT_VARIATION_DIALOG_NAME")} - {variationName}";
    this.SetOptionList(options);
    this.m_options = new ArrayList();
    for (int index = 0; index < options.Count; ++index)
      this.m_options.Add(options[index]);
    this.m_deleteBtn.Enabled = false;
  }

  private void CUiMFVariationsOptionsDlg_Load(object sender, EventArgs e)
  {
    this.editBox = new TextBox();
    this.editBox.Location = new Point(0, 0);
    this.editBox.Size = new Size(0, 0);
    this.editBox.Hide();
    this.m_optionsList.Controls.AddRange(new Control[1]
    {
      (Control) this.editBox
    });
    this.editBox.Text = "";
    this.editBox.BorderStyle = BorderStyle.FixedSingle;
  }

  private void m_optionsList_KeyPress(object sender, KeyPressEventArgs e)
  {
    if (e.KeyChar != '\r')
      return;
    this.CreateEditBox(sender);
  }

  private void m_optionsList_DoubleClick(object sender, EventArgs e) => this.CreateEditBox(sender);

  private void m_optionsList_KeyDown(object sender, KeyEventArgs e)
  {
    if (e.KeyData != Keys.F2)
      return;
    this.CreateEditBox(sender);
  }

  private void SetOptionList(ArrayList options)
  {
    for (int index = 0; index < options.Count; ++index)
      this.m_optionsList.Items.Add((object) ((CApMFVariationOption) options[index]).Name);
  }

  private void m_newBtn_Click(object sender, EventArgs e)
  {
    string nameWithEnumeration = this.CreateNameWithEnumeration();
    this.m_optionsList.Items.Add((object) nameWithEnumeration);
    this.m_options.Add((object) new CApMFVariationOption(0, 0, nameWithEnumeration));
    if (this.m_options.Count <= 0)
      return;
    this.m_ok.Enabled = true;
  }

  private string CreateNameWithEnumeration()
  {
    for (int index1 = 1; index1 < 1000; ++index1)
    {
      bool flag = false;
      string nameWithEnumeration = "Variation_" + index1.ToString();
      for (int index2 = 0; index2 < this.m_options.Count && !flag; ++index2)
      {
        if (((CApMFVariationOption) this.m_options[index2]).Name.Equals(nameWithEnumeration))
          flag = true;
      }
      if (!flag)
        return nameWithEnumeration;
    }
    return "Variation";
  }

  private void m_deleteBtn_Click(object sender, EventArgs e)
  {
    int selectedIndex = this.m_optionsList.SelectedIndex;
    this.m_optionsList.Items.RemoveAt(selectedIndex);
    this.m_options.RemoveAt(selectedIndex);
    if (this.m_options.Count != 0)
      return;
    this.m_ok.Enabled = false;
  }

  private void CreateEditBox(object sender)
  {
    if (this.m_optionsList.SelectedIndex < 0 || this.m_optionsList.SelectedIndex >= this.m_optionsList.Items.Count)
      return;
    int num = 1;
    this.m_optionsList = (ListBox) sender;
    this.m_itemSelected = this.m_optionsList.SelectedIndex;
    Rectangle itemRectangle = this.m_optionsList.GetItemRectangle(this.m_itemSelected);
    string str = (string) this.m_optionsList.Items[this.m_itemSelected];
    this.editBox.Location = new Point(itemRectangle.X + num, itemRectangle.Y + num);
    this.editBox.Size = new Size(itemRectangle.Width - 10, itemRectangle.Height - num);
    this.editBox.Show();
    this.m_optionsList.Controls.AddRange(new Control[1]
    {
      (Control) this.editBox
    });
    this.editBox.Text = str;
    this.editBox.Focus();
    this.editBox.SelectAll();
    this.editBox.KeyPress += new KeyPressEventHandler(this.EditOver);
    this.editBox.LostFocus += new EventHandler(this.FocusOver);
  }

  private void FocusOver(object sender, EventArgs e)
  {
    this.m_optionsList.Items[this.m_itemSelected] = (object) this.editBox.Text;
    ((CApMFVariationOption) this.m_options[this.m_itemSelected]).Name = this.editBox.Text;
    this.editBox.Hide();
  }

  private void EditOver(object sender, KeyPressEventArgs e)
  {
    if (e.KeyChar != '\r')
      return;
    this.m_optionsList.Items[this.m_itemSelected] = (object) this.editBox.Text;
    ((CApMFVariationOption) this.m_options[this.m_itemSelected]).Name = this.editBox.Text;
    this.editBox.Hide();
  }

  private void m_ok_Click(object sender, EventArgs e)
  {
    if (this.AreNamesUnique())
    {
      ((Form) this).DialogResult = DialogResult.OK;
      ((Form) this).Close();
    }
    else
    {
      int num = (int) TxMessageBox.Show(CApMFAdminUtils.m_resourceManager.GetString("VARIATION_OPTIONS_NOT_UNIQUE_ERROR_MESSAGE"), CApMFAdminUtils.m_resourceManager.GetString("CMD_CONFIGURATION_ADMINISTRATOR_NAME"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
  }

  private bool AreNamesUnique()
  {
    bool flag = true;
    for (int index1 = 0; index1 < this.m_options.Count & flag; ++index1)
    {
      for (int index2 = 0; index2 < this.m_options.Count & flag; ++index2)
      {
        string name1 = ((CApMFVariationOption) this.m_options[index1]).Name;
        string name2 = ((CApMFVariationOption) this.m_options[index2]).Name;
        if (index1 != index2 && name1.Equals(name2))
          flag = false;
      }
    }
    return flag;
  }

  private void m_cancel_Click(object sender, EventArgs e) => ((Form) this).Close();

  private void m_optionsList_SelectedIndexChanged(object sender, EventArgs e)
  {
    if (this.m_optionsList.SelectedIndex > -1 && this.m_optionsList.SelectedIndex <= this.m_optionsList.Items.Count)
      this.m_deleteBtn.Enabled = true;
    else
      this.m_deleteBtn.Enabled = false;
  }

  internal ArrayList Options => this.m_options;

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiMFVariationsOptionsDlg));
    this.m_optionsList = new ListBox();
    this.m_newBtn = new Button();
    this.m_deleteBtn = new Button();
    this.m_ok = new Button();
    this.m_cancel = new Button();
    ((Control) this).SuspendLayout();
    this.m_optionsList.FormattingEnabled = true;
    this.m_optionsList.Location = new Point(12, 12);
    this.m_optionsList.Name = "m_optionsList";
    this.m_optionsList.Size = new Size(120, 147);
    this.m_optionsList.TabIndex = 0;
    this.m_optionsList.SelectedIndexChanged += new EventHandler(this.m_optionsList_SelectedIndexChanged);
    this.m_optionsList.DoubleClick += new EventHandler(this.m_optionsList_DoubleClick);
    this.m_optionsList.KeyPress += new KeyPressEventHandler(this.m_optionsList_KeyPress);
    this.m_optionsList.KeyDown += new KeyEventHandler(this.m_optionsList_KeyDown);
    this.m_newBtn.Location = new Point(138, 12);
    this.m_newBtn.Name = "m_newBtn";
    this.m_newBtn.Size = new Size(75, 23);
    this.m_newBtn.TabIndex = 1;
    this.m_newBtn.Text = "New";
    this.m_newBtn.UseVisualStyleBackColor = true;
    this.m_newBtn.Click += new EventHandler(this.m_newBtn_Click);
    this.m_deleteBtn.Location = new Point(138, 41);
    this.m_deleteBtn.Name = "m_deleteBtn";
    this.m_deleteBtn.Size = new Size(75, 23);
    this.m_deleteBtn.TabIndex = 3;
    this.m_deleteBtn.Text = "Delete";
    this.m_deleteBtn.UseVisualStyleBackColor = true;
    this.m_deleteBtn.Click += new EventHandler(this.m_deleteBtn_Click);
    this.m_ok.Location = new Point(57, 169);
    this.m_ok.Name = "m_ok";
    this.m_ok.Size = new Size(75, 23);
    this.m_ok.TabIndex = 4;
    this.m_ok.Text = "OK";
    this.m_ok.UseVisualStyleBackColor = true;
    this.m_ok.Click += new EventHandler(this.m_ok_Click);
    this.m_cancel.DialogResult = DialogResult.Cancel;
    this.m_cancel.Location = new Point(138, 169);
    this.m_cancel.Name = "m_cancel";
    this.m_cancel.Size = new Size(75, 23);
    this.m_cancel.TabIndex = 5;
    this.m_cancel.Text = "Cancel";
    this.m_cancel.UseVisualStyleBackColor = true;
    this.m_cancel.Click += new EventHandler(this.m_cancel_Click);
    ((ContainerControl) this).AutoScaleDimensions = new SizeF(6f, 13f);
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Form) this).ClientSize = new Size(219, 201);
    ((Control) this).Controls.Add((Control) this.m_cancel);
    ((Control) this).Controls.Add((Control) this.m_ok);
    ((Control) this).Controls.Add((Control) this.m_deleteBtn);
    ((Control) this).Controls.Add((Control) this.m_newBtn);
    ((Control) this).Controls.Add((Control) this.m_optionsList);
    ((Form) this).FormBorderStyle = FormBorderStyle.FixedDialog;
    ((Form) this).Icon = (Icon) componentResourceManager.GetObject("$this.Icon");
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiMFVariationsOptionsDlg);
    ((Control) this).Text = "Variation Options";
    ((Form) this).ShowIcon = false;
    ((Form) this).Load += new EventHandler(this.CUiMFVariationsOptionsDlg_Load);
    ((Control) this).ResumeLayout(false);
  }
}
